// 滚动条设置
$(".slide-list").mCustomScrollbar();
$(".m").mCustomScrollbar();

// select2
$('.select2').select2();
$('.multiselect').multiselect();

// 日历格式设置
$.datetimepicker.setLocale('ch');
$('.datetimepicker').datetimepicker({
    format: 'Y-m-d H:i',
});
$('.datepicker').datetimepicker({
    format: 'Y-m-d',
});

// 通用模态框确认操作
var common_ids = [];
var common_type = 0;      // 0：单个操作，1：批量操作
function before_set_common(that){
  return true;
}
function set_common_modal(that,type){
  if(!before_set_common(that)){
    return;
  }
  var title = $(that).attr('data-title');
  var hint = $(that).attr('data-hint');
  var url = $(that).attr('data-url');
  var val = $(that).attr('data-value');
  var idname = $(that).attr('data-idname') || 'id';
  $('#common_modal').children('h3').text(title);
  $('#common_modal').children('h4').text(hint);
  common_type = type;
  if(type==0){
    var id = $(that).attr('data-id');
    $('#common_id').val(id);
  }
  $('#common_url').val(url);
  $('#common_idname').val(idname);
  $('#common_val').val(val);
}
function common_sure(){
  $('.btn').attr('disabled',true).css('cursor','not-allowed');
  $('.btn').attr('title','请勿重复点击！')
  var cid;
  var method;
  var url = $('#common_url').val();
  var idname = $('#common_idname').val();
  var val = $('#common_val').val();
  if(common_type==0){
    cid = $('#common_id').val();
  }else{
    common_ids = [];
    $('.check_div.checked').each(function(){
      if(!$(this).hasClass('allcheck')){
        var id = $(this).attr('data');
        common_ids.push(id);
      }
    });
    if(common_ids.length){
      cid = common_ids;
    }else{
      alert('未选择任何记录');
      $('#common_modal').modal('hide');
      $('.btn').attr('disabled',false).css('cursor','pointer');
      return;
    }
  }
  if(val == 'bookmark_detail_task'){
      method = 'get'
  }else if(val == ''){
      method = 'post'
  }else{
      method = 'put'
  }

  data = {'del_type':common_type};
  data[idname] = cid;
  data['val'] = val;
  $.ajax({
      url: url,
      type: method,
      data: data,
      success:function(msg){
          window.location.reload();
          $('.btn').attr('disabled',false).css('cursor','pointer');
      }
  })
}

// 通用编辑
function CommonModify(data,form_id){
    for(key in data){
        $('#'+form_id+' select[name="'+key+'"]').val(data[key]);
        $('#'+form_id+' input[name="'+key+'"]').val(data[key]);
        $('#'+form_id+' textarea[name="'+key+'"]').val(data[key]);
    }
};
// 清空表单
function reset_form(form_id){
  $('#'+form_id+' select').val('');
  $('#'+form_id+' input').val('');
  $('#'+form_id+' textarea').val('');
}

// 通用查询
function search_sub(){
    var data = $('.table_filter').serialize();
    var serializeObj = {};
    var that = $('.table_filter');
    $(that.serializeArray()).each(function () {
        serializeObj[this.name] = this.value;
    });
    var url = document.location.toString();
    var arrObj = url.split("?");
    if (arrObj.length > 1) {
        var arrPara = arrObj[1].split("&");
        var arr;
        for (var i = 0; i < arrPara.length; i++) {
            arr = arrPara[i].split("=");
            if(!serializeObj.hasOwnProperty(arr[0]) && arr[1]){
                serializeObj[arr[0]] = arr[1];
                if(arr[0] != 'msg_id'){
                    data = data+'&'+arr[0]+'='+arr[1]
                }
            }
        }
    }
    window.location.href = '?' + data;
}

// 表格排序
function table_sort(name,val){
  var next = 'D';
  if(val=='D'){
    next = 'A';
  }else if(val=='A'){
    next = 'D';
  }
  $('input[name="table_sort"]').val(name+'-'+next);
  search_sub();
}

// excel导出通用方法
function export_excel(e){
  heads = [];
  keys = [];
  $('#export_head th').each(function(){
    var key = $(this).attr('data-key');
    if(key){
      heads.push($(this).text());
      keys.push(key);
    }
  });
  heads = heads.join(',');
  keys = keys.join(',');
  var data = {'heads':heads,'keys':keys,'excel':true};
  $($('.table_filter').serializeArray()).each(function(){
       data[this.name]=this.value;
  });
  var export_btn = $(e);
  export_btn.text('导出中...').attr('disabled','disabled').addClass('btn_loading').css('cursor','not-allowed');
  $.ajax({
      url :'',
      method:'GET',
      data:data,
      success:function(res){
          if (res['status']){
              window.location.href = res.url;
              export_btn.text('导出').removeAttr('disabled').removeClass('btn_loading').css('cursor','pointer');
          }else{
              alert(res['msg']);
              export_btn.text('导出').removeAttr('disabled').removeClass('btn_loading').css('cursor','pointer');
          }
      },
      error:function(error){
        console.log(error);
        export_btn.text('导出').removeAttr('disabled').removeClass('btn_loading').css('cursor','pointer');
        alert('导出出错')
      }
  });
}

// 获取get参数对象
function GetRequest() {
   var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
         theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}
// 翻页
function go_page(num){
  var obj = GetRequest();
  if(!num){
    num = $('#go_page').val();
    var max = $('#go_page').attr('data-max');
    if(!num || parseInt(num) > parseInt(max)|| parseInt(num) <= 0){
      return false;
    }
  }
  var model_type = $('input[name="model_type"]').val();
  if(model_type){
    obj.model_type = model_type;
  }
  obj.page = num;
  obj_list = []
  for(i in obj){
    obj_list.push(i+'='+obj[i]);
  }
  window.location.href = '?'+obj_list.join('&');
}

// cookie设置前页及名称
var titlename = $('meta[name="titlename"]').attr('content');
if(titlename){
  document.cookie = 'last_url="'+window.location.href+'";path=/';
  document.cookie = 'last_name="'+titlename+'";path=/';
}
// 点击菜单清空
$('.header li,.content_title,.thrid a').click(function(){
  document.cookie = 'urlhistory=[];path=/';
  document.cookie = 'last_url="";path=/';
  document.cookie = 'last_name="";path=/';
})

// 通用搜索标红
function mark_red(sea){
    sea = sea||'search';
    // 默认input里面name参数为search，
    var search = $('input[name="'+ sea +'"]').val();
    if(search){
        $('.mark-red').each(function(){
            var text = $(this).html();
            if(text.indexOf(search)!=-1 || text.indexOf(search = search.toUpperCase())!=-1 || text.indexOf(search = search.toLowerCase())!=-1){
                var reg = new RegExp(search, "gi");
                var new_text = text.replace(reg,'<span class="red_font">'+search+'</span>');
                $(this).html(new_text);
            }
        })
    }
}
mark_red();
